package team.nmsg.ge.system.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import team.nmsg.ge.system.util.ServerParams;

import com.alibaba.fastjson.JSONObject;

/**
 * @userg
 * 
 * 统一异常拦截
 *
 */
@ControllerAdvice
public class SysControllerExceptionHandler extends BaseController{
	
	private static Logger log = LoggerFactory.getLogger(SysControllerExceptionHandler.class);
	
	
	@ExceptionHandler(value = Exception.class)
	public ModelAndView defaultErrorHandler(HttpServletRequest req, HttpServletResponse resp,Exception e)  {	
		
		
		//没有权限
		if( e instanceof AuthorizationException || e instanceof UnauthorizedException ){
			log.error("NMSControllerExceptionHandler:::no permit :::  "+e.getMessage());
//			if( isAjaxRequest(req) ){
				JSONObject jsonObject = new JSONObject();
				jsonObject.put( ServerParams.NOPERMIT_KEY, true);
				writeJson(jsonObject  ,  resp );
				return null;
//			}else{
//				ModelAndView mv = new ModelAndView("error/401");
//				return mv;
//			}
		}else{
			log.error(" NMSControllerExceptionHandler ::: " , e );
			//Server error
//			if( isAjaxRequest(req) ){
				JSONObject jsonObject = new JSONObject();
				jsonObject.put( ServerParams.SERVER_ERROR_KEY, true);
				writeJson(jsonObject  ,  resp );
//			}else{
//				ModelAndView mv = new ModelAndView("error/500");
//				return mv;
//			}
			
		}
		return null;
	 }

	
	
}
